// pages/feedback/feedback.js
const db = wx.cloud.database()
let reg_rel = /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 被选中的图片数组
    chooseImage: [],
    // 文本域的内容
    textValue: "",
    // 外网的图片的路径数组
    UpLoadImgs: [],
    // 控制弹出窗显示
    popup:false,
    //每个图片的id
    img_id:0,
    textarea:'',
    phone:'',
    

  },
  //texteara防抖函数
  area_value(e){
    console.log(e.detail.value)
    clearTimeout(time)
    let time = setTimeout(()=>{
      this.setData({
        textarea:e.detail.value
      })
    },1000)

  },
  //input
  input_value(e){
    console.log(e.detail.value)
    const {value} = e.detail
    if(value){
      if(reg_rel.test(value)){
        clearTimeout(time)
        let time = setTimeout(()=>{
          this.setData({
           phone:value
          })
        },1000)
        console.log('手机号验证成功')
      }else{
        console.log('手机号验证错误')
      }
    }
    
  },
 // 点击加号选择图片
  add_img(){
    let that = this
    wx.chooseImage({
      count: 3,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        that.setData({
          chooseImage:[...that.data.chooseImage,...tempFilePaths]
        })
        console.log(res)
      }
    })
  },
// 点击选中的图片的下弹出窗  
  img_popup(e){
    this.setData({
      popup:true,
      img_id:e.currentTarget.dataset.id
    })
  },
// 替换选中的图片
  att_img(){
    let id = this.data.img_id
    let that = this
    wx.chooseImage({
      count: 3,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        let chooseImage = that.data.chooseImage
        chooseImage.splice(id,1,tempFilePaths)
        that.setData({
          chooseImage,
          popup:false
        })
        console.log(res)
      }
    })
  },
 // 删除选中的图片
  del_img(){
    const id = this.data.img_id
    let chooseImage =  this.data.chooseImage
    chooseImage.splice(id,1)
    this.setData({
      chooseImage,
      popup:false
    })
  },
// 取消选中的图片  
  cancle_img(){
    this.setData({
      popup:false
    })
  },
//提交意见函数
  submit(){
    if(!this.data.textarea.trim()){
      console.log("没写意见呢")
    }else if(!reg_rel.test(this.data.phone)){
      console.log("验证失败了")
    }else{
      clearTimeout()
      setTimeout(()=>{
        db.collection('advice').add({
          data:{
            name:this.data.textarea,
            image:this.data.chooseImage,
            phone:this.data.phone
          }
        })
        this.setData({
          phone:'',
          textarea:'',
          chooseImage:[]
        })
      },1000)
      
    }
    
  }
})
